<?php

class ContactController extends Zend_Controller_Action {

    public function init() {
        $this->_statusMessenger = $this->_helper->flashMessenger;
        $this->_errorMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
        $this->_errorMessenger->setNamespace('error');
        $this->view->statusMessages = $this->_statusMessenger->getMessages();
        $this->view->errorMessages = $this->_errorMessenger->getMessages();
    }

    public function indexAction() {
        $this->view->headTitle($this->view->translate('Contact'));
        $this->view->documentClasses = array('contact');

        // Create form instance
        $this->view->form = $this->_getContactForm();

        $post = $this->_request->getPost();

        if ($post) {
            if (!$this->view->form->isValid($this->_request->getParams())) {
                $this->view->form->populate($this->_request->getParams());
            } else {


                $values = $this->_request->getParams();

                $configOptions = $this->getInvokeArg('bootstrap')->getOptions();
                
                $config = array('auth' => 'login',
                    'username' => $configOptions['contact']['user'],
                    'password' => $configOptions['contact']['password']);

                $transport = new Zend_Mail_Transport_Smtp('smtp.bekloppter.name', $config);


                $mail = new Zend_Mail();
                $mail->setBodyText($values['message']);
                $mail->setFrom($values['email'], $values['name']);
                $mail->addTo($configOptions['contact']['email'], 'Kontaktformular Scoring');
                $mail->setSubject($post['subject']);
                try {
                    $mail->send($transport);
                } catch (Exception $e) {
                    $this->_errorMessenger->addMessage(
                            $this->view->translate('Error while sending!')
                    );

                    $this->_redirect('/contact');
                }
                $this->_statusMessenger->addMessage(
                        $this->view->translate('Message send.'));
                $this->_redirect('/index');
            }
        }
    }

    /**
     * Erstellt KontaktFormular
     *
     * @return \Zend_Form 
     *
     */
    private function _getContactForm() {


        $form = new Zend_Form();
        $form->setMethod(Zend_Form::METHOD_POST);

        $form->addElement('text', 'name', array(
            'label' => _('Name'),
            'placeholder' => _('What should we call you?'),
            'size' => 32,
            'required' => true,
        ));

        $form->addElement('text', 'email', array(
            'label' => 'E-Mail',
            'placeholder' => _('your email address is required'),
            'size' => 32,
            'required' => true,
            'validators' => array(
                'EmailAddress'
            )
        ));

        $form->addElement('text', 'subject', array(
            'label' => _('Subject'),
            'placeholder' => _('i love you guys'),
            'size' => 32,
            'required' => true,
        ));

        $form->addElement('textarea', 'message', array(
            'label' => _('Message'),
            'placeholder' => _('what a nice little project. keep it up!'),
            'cols' => '40',
            'rows' => '4',
            'required' => true,
        ));

        $form->addElement('submit', 'submit-contact', array(
            'label' => _('send'),
            'class' => 'contact_send',
            'ignore' => true,
        ));

        if (Zend_Auth::getInstance()->hasIdentity()) {
            $form->populate(array('name' => Zend_Auth::getInstance()->getIdentity()->salutation, 'email' => Zend_Auth::getInstance()->getIdentity()->email));
        }
        $this->translatePlaceholder($form);
        return $form;
    }

    /**
     * Übersetzt die placeholder in der From 
     * @param type $form ZendFrom 
     */
    private function translatePlaceholder($form) {
        foreach ($form->getElements() as $key => $element) {
            $placeholder = $element->getAttrib('placeholder');
            if (isset($placeholder)) {
                $form->$key->setAttrib('placeholder', $this->view->translate($placeholder));
            }
        }
    }

}

